// $Id: loadingbar.cpp 470 2008-05-27 16:27:22Z agjermun $
// Loadingbar class implementation
//
// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "loadingbar.h"

#include <OgreOverlayManager.h>
#include <OgreOverlayElement.h>

/**
 * constructor which takes the RenderWindow as input
 * @param win the RenderWindow in which the LoadingBar is shown
*/
LoadingBar::LoadingBar(RenderWindow *win)
{
	mInitProportion = 0.70f;
	mNumGroupsLoad = 1;
	mNumGroupsInit = 1;

	mWindow = win;
}

/**
 * default constructor
*/
LoadingBar::~LoadingBar()
{

}

/**
 * initialize the loading bar
*/
void LoadingBar::init()
{
	OverlayManager::getSingletonPtr()->getByName("Core/LoadOverlay")->show();
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->setWidth(0);
	mProgressBarMaxSize = OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar")->getWidth();
}

/**
 * hide the loading bar from the overlay
*/
void LoadingBar::hide()
{
	OverlayManager::getSingletonPtr()->getByName("Core/LoadOverlay")->hide();
}

/**
 * sets state to parsing of resource scripts (description)
 * @param groupName resource group name
 * @param scriptCount number of scripts
*/
void LoadingBar::resourceGroupScriptingStarted(const String &groupName, size_t scriptCount)
{
	// Lets assume script loading is 70%
	mProgressBarInc = mProgressBarMaxSize * mInitProportion / (float)scriptCount;
	mProgressBarInc /= mNumGroupsInit;
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Description")->setCaption("Parsing scripts...");
	mWindow->update();
}

/**
 * sets state to parsing of resource scripts (comment)
 * @param scriptName name of the running script
*/
void LoadingBar::scriptParseStarted(const String &scriptName)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Comment")->setCaption(scriptName);
	mWindow->update();
}

/**
 * sets state to end of resource scripts parsing
 * @param scriptName name of the script that has finished
*/
void LoadingBar::scriptParseEnded(const String &scriptName)
{
	Real newWidth =  OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->getWidth() + mProgressBarInc;
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->setWidth(newWidth);
	mWindow->update();
}

/**
 * Not implemented
 * @param groupName resource group name
*/
void LoadingBar::resourceGroupScriptingEnded(const String &groupName)
{

}

/**
 * sets state to loading of resource groups (description)
 * @param groupName resource group name
 * @param resourceCount number of resources
*/
void LoadingBar::resourceGroupLoadStarted(const String &groupName, size_t resourceCount)
{
	mProgressBarInc = mProgressBarMaxSize * (1 - mInitProportion) / (float)resourceCount;
	mProgressBarInc /= mNumGroupsLoad;
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Description")->setCaption("Loading resources...");
	mWindow->update();
}

/**
 * sets state to loading of resource (comment)
 * @param resource pointer to the resource being loaded
*/
void LoadingBar::resourceLoadStarted(const ResourcePtr &resource)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Comment")->setCaption("Loading resources...");
	mWindow->update();
}

/**
 * Not implemented.
*/
void LoadingBar::resourceLoadEnded(void)
{
}

/**
 * sets state to world geometry stage (description)
 * @param description the description to show on screen
*/
void LoadingBar::worldGeometryStageStarted(const String &description)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Comment")->setCaption(description);
	mWindow->update();
}

/**
 * sets state to end of world geometry stage
*/
void LoadingBar::worldGeometryStageEnded(void)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->setWidth( 
		(OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->getWidth() +
        mProgressBarInc));

	mWindow->update();
}

/**
 * Not implemented
*/
void LoadingBar::resourceGroupLoadEnded(const String &groupName)
{
}

/**
 * sets the comment field of the loading bar to the given string
 * @param comment the text string to display in the comment field
*/
void LoadingBar::setComment(const String &comment)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Comment")->setCaption(comment);
	mWindow->update();
}

/**
 * sets the description field of the loading bar to the given string
 * @param description the text string to display in the description field
*/
void LoadingBar::setDescription(const String &description)
{
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Description")->setCaption(description);
	mWindow->update();
}

/**
 * set the progress for something which is not a script
 * @param progress progress value
*/
void LoadingBar::setNonScriptProgress(Real progress)
{
	Real newWidth =  (mInitProportion + (progress * (1 - mInitProportion))) * mProgressBarMaxSize;
	OverlayManager::getSingletonPtr()->getOverlayElement("Core/LoadPanel/Bar/Progress")->setWidth(newWidth);
	mWindow->update();
}
